using System;
using System.Collections.Generic;
using System.Text;
using iTunesLib;
using Rapier.Kodachi.Shared;
using System.Drawing;

namespace Rapier.Kodachi.iTunes.Actions
{
    public class VolumeSet : iTunesActionBase
    {
        #region Constructor
        public VolumeSet(Plugin plugin)
            : base(plugin, "Rapier.Kodachi.iTunes.VolumeControl", "Volume control", "Set the volume in iTunes to a percent (0-100).")
        {
            DefaultIndirectTextMode = true;
            IndirectItemUse = IndirectItemUse.Required;
            GetImage = delegate() { return volumeMuteImage; };
        }
        #endregion

        #region Action Members
        public override bool IsValidIndirect(Item indirectItem)
        {
            if (indirectItem.Properties.ContainsKey("Text"))
                return Microsoft.VisualBasic.Information.IsNumeric(indirectItem.Properties["Text"].ToString());
            else
                return false;
        }

        public override void Execute(Item[] directItems, Item[] indirectItems)
        {
            if (base.IsItunesRunning())
            {
                if (indirectItems.Length > 0)
                {
                    try
                    {
                        int volume;
                        if (Int32.TryParse(indirectItems[0].Properties["Text"].ToString(), out volume))
                        {
                            if (volume < 0) volume = 0;
                            else if (volume > 100) volume = 100;
                            itunes.SoundVolume = volume;
                        }
                    }
                    catch
                    {
                    }
                }
            }
        }
        #endregion
    }
}
